package com.aaa.service.impl;

import com.aaa.dao.PermissionDao;
import com.aaa.entity.Permission;

import com.aaa.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 祝云松
* @description 针对表【e_permission(左侧菜单地址)】的数据库操作Service实现
* @createDate 2023-02-24 17:28:37
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, Permission> implements PermissionService{


    @Autowired
    PermissionDao permissionDao;
    @Override
    public List<Permission> selectPermissionByEmpId(Integer id) {
        return permissionDao.selectPermissionByEmpId(id);
    }

    @Override
    public List<Permission> gettoken(String username) {
        List<Permission> list=permissionDao.gettoken(username);
        List<Permission> firsts = firsts(list);
        return firsts;
    }
    private List<Permission> firsts(List<Permission> permissions){
        //1.一级菜单
        List<Permission> firsts=new ArrayList<>();
        for (Permission permission:permissions){
            System.out.println("permissions = " + permissions);
            if(permission.getPid().equals("0")){
                firsts.add(permission);
            }
        }
        //2.查询一级菜单下的二级菜单
        for(Permission first:firsts){
            List<Permission> children=new ArrayList<>();
            for(Permission p:permissions){
                if(first.getId().equals(Integer.valueOf(p.getPid()))){
                    children.add(p);
                }
            }
            first.setChildren(children);
        }
        return firsts;
    }
}




